package com.wanmait.wanmarecruit.aop;

import com.wanmait.wanmarecruit.annotation.ManageLogAnnotation;
import com.wanmait.wanmarecruit.entity.Manager;
import com.wanmait.wanmarecruit.entity.ManagerLog;
import com.wanmait.wanmarecruit.service.ManagerLogService;
import com.wanmait.wanmarecruit.service.ManagerService;
import com.wanmait.wanmarecruit.util.JWTUtils;
import com.wanmait.wanmarecruit.util.SystemContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Arrays;

@Component
@Aspect
public class ManageLogAop {

    @Resource
    private ManagerService managerService;
    @Resource
    private ManagerLogService managerLogService;
    @Pointcut("@annotation(com.wanmait.wanmarecruit.annotation.ManageLogAnnotation)")
    public void pointCut(){}

    @AfterReturning(value="pointCut()",returning = "returnVal")
    public void doAfter(JoinPoint joinPoint,Object returnVal)
    {
        String methodName = joinPoint.getSignature().getName();
        Method method = this.currentMethod(joinPoint,methodName);
        ManageLogAnnotation manageLogAnnotation = method.getAnnotation(ManageLogAnnotation.class);
        HttpServletRequest request = SystemContext.getRequest();
        String token = request.getHeader("token");
        Integer id = Integer.parseInt(JWTUtils.getAudience(token));
        Manager manager = managerService.getById(id);
        ManagerLog managerLog = new ManagerLog();
        managerLog.setManagerId(id);
        managerLog.setIp(request.getRemoteAddr());
        StringBuilder info = new StringBuilder();
        info.append("目标类:").append(joinPoint.getTarget().getClass().getName()).append(";");
        info.append("方法：").append(joinPoint.getSignature().getName()).append(";");
        info.append("参数：").append(Arrays.toString(joinPoint.getArgs())).append(";");
        if (returnVal!=null)
        {
            info.append("返回类型：").append(returnVal.getClass().getName()).append(";");
            info.append("返回值").append(returnVal.getClass().getName()).append(";");
        }

        managerLog.setInfo(info.toString());
        managerLog.setIntroduction(manager.getName()+"在"+manageLogAnnotation.module()+"里"+manageLogAnnotation.info());
        managerLogService.save(managerLog);
    }

    private Method currentMethod(JoinPoint joinPoint, String methodName) {
        /**
         * 获取目标类的所有方法，找到当前要执行的方法
         */
        Method[] methods = joinPoint.getTarget().getClass().getMethods();
        Method resultMethod = null;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                resultMethod = method;
                break;
            }
        }
        return resultMethod;
    }
}
